﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;


namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucAirConditionImmediateImpact.xaml
    /// </summary>
    public partial class ucAirConditionImmediateImpact : UserControl
    {
        public ucAirConditionImmediateImpact()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
            
        }

        public void loadData()
        {
            cbxDescription.ItemsSource = LibraryManagerInCom.ImmediateImpactList;
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;

            cbxDirection.ItemsSource = LibraryManagerInCom.GlassDirectionList;
            cbxDirection.DisplayMemberPath = "glass_direction";
            cbxDirection.SelectedIndex = 0;

        }

        public void reload()
        {
            cbxDescription.ItemsSource = null;
            cbxDescription.Items.Clear();
            foreach (var item in LibraryManagerInCom.ImmediateImpactList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_immediate_impact im = (tb_immediate_impact)cbxDescription.SelectedItem;
            if (im != null)
            {
                cbxDirection.SelectedIndex = Convert.ToInt16(im.direction_id) - 1;
                cbxTime.SelectedIndex = Convert.ToInt16(im.time) - 1;
                txtPressure.Text = im.weight_per_meter_square.ToString();
                txtCoefficient.Text = im.coefficient.ToString();
            }
                   
        }

        public void newData()
        {
            cbxDescription.Text = "";
            cbxDirection.SelectedIndex = 0;
            cbxTime.SelectedIndex = 0;
            txtCoefficient.Text = "";
            txtPressure.Text = "";
        }
        public void saveData()
        {
            tb_immediate_impact im = new tb_immediate_impact();
            im.description = cbxDescription.Text;
            im.direction_id = cbxDirection.SelectedIndex + 1;
            im.time = cbxTime.SelectedIndex + 1;
            im.weight_per_meter_square = Convert.ToInt16(txtPressure.Text);
            im.coefficient = Convert.ToDouble(txtCoefficient.Text);
            LibraryManagerInCom.addImmediateImpact(im);
            reload();
        }
        public void delData()
        {
            tb_immediate_impact im = (tb_immediate_impact)cbxDescription.SelectedItem;
            LibraryManagerInCom.delImmediateImpact(im);
            reload();
        }
        public void copyData()
        {
            tb_immediate_impact im = (tb_immediate_impact)cbxDescription.SelectedItem;
            tb_immediate_impact im2 = LibraryManagerInCom.copyImmediateImpact(im);
            cbxDescription.Text = im2.description;
            cbxDirection.SelectedIndex = Convert.ToInt16(im2.direction_id) - 1;
            cbxTime.SelectedIndex = Convert.ToInt16(im2.time) - 1;
            txtPressure.Text = im2.weight_per_meter_square.ToString();
            txtCoefficient.Text = im2.coefficient.ToString();

        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
                
    }
}
